package com.ikkong.shine.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.shine.model.Doctor;
import com.ikkong.shine.service.DoctorService;
import com.ikkong.shine.service.impl.DoctorServiceImpl;
import com.ikkong.system.controller.base.UrlPermissController;
import com.jfinal.kit.StrKit;

/**
 * Generated by Blade.
 * 2017-03-17 11:02:35
 */
public class DoctorController extends UrlPermissController {
	private static String CODE = "doctor";
	private static String PERFIX = "doctor";
	private static String LIST_SOURCE = "Doctor.list";
	private static String VIEW_SOURCE="Doctor.view";
	private static String BASE_PATH = "/platform/doctor/";
	
	DoctorService service = new DoctorServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "doctor.html");
	}

	public void add() {
		//查询所属数据源
		List<Record> list=service.findByDbSqlName();
		setAttr("list", list);
		setAttr("code", CODE);
		render(BASE_PATH + "doctor_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Doctor doctor = service.findById(id);
		setAttr("model", JsonKit.toJson(doctor));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "doctor_edit.html");
	}

	public void view() {
		String id = getPara(0);
		@SuppressWarnings("unchecked")
		Map<String, Object> map=Blade.dao().selectSingle(VIEW_SOURCE, Record.create().set("doctor_id", id), Map.class);
		//Doctor doctor = service.findById(id);
		setAttr("model", JsonKit.toJson(map));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "doctor_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	//医生信息添加
	public void save() {
		Doctor doctor = mapping(PERFIX, Doctor.class);
		
		Doctor doctor_login_id=service.findByLogin_id(doctor.getLogin_id());
		
		if(null!=doctor_login_id&&doctor_login_id.getDoctor_id()!=null){
			renderJson(error("当前："+doctor.getDoctor_id()+"已存在！"));
			return;
		}
		
		boolean temp=service.save(doctor);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Doctor doctor = mapping(PERFIX, Doctor.class);
		boolean temp = service.update(doctor);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		//Rlt_doctor2queue_typeService rservice=new Rlt_doctor2queue_typeServiceImpl();
		
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
	
	public void save_doctor(){
		/*医生编号	唯一标识	必要	login_id	Varchar(100)	NOT NULL	医生登录使用的ID：（门诊医生取医生工作站登录id，检查科室医生取工作站登录id或各自熟知的工号）
		姓名		必要	name	Varchar(50)	NOT NULL	医生姓名
		医生所属科室			department	Varchar(50)		医生所属科室（页面显示使用；当存在多个所属科室时，取主科室）
		职称			title	Varchar(50)		医生职位（主任、副主任、主治医师等）*/
		Map<String, Object> map=new HashMap<String, Object>();
		String doctor_strs= getPara("doctors");
		if(StrKit.isBlank(doctor_strs)){
			map.put("code", -1);
			map.put("msg", "请传值");
			renderJson(map);
			return;
		}else{
			JSONArray doctorList= JSONArray.parseArray(doctor_strs);
			if(doctorList !=null){
				Doctor doctor= null;
				for (Object json : doctorList) {
					JSONObject doctor_json = (JSONObject) json;
					doctor=new Doctor();
					Doctor doctor_login_id=service.findByLogin_id(doctor_json.getString("login_id"));
					if(null!=doctor_login_id&&doctor_login_id.getDoctor_id()!=null){
						boolean bool= service.updateBy("name='"+doctor_json.getString("name")+"',department='"+doctor_json.getString("department")+"',title='"+doctor_json.getString("title")+"'", " where login_id in('"+doctor_json.getString("login_id")+"')", null);
						if(bool){
							map.put("code", 200);
							map.put("msg", "医生信息修改成功");
							renderJson(map);
						}
						return;
					}else{
						doctor.setLogin_id(doctor_json.getString("login_id"));
						doctor.setName(doctor_json.getString("name"));
						doctor.setTitle(doctor_json.getString("title"));
						doctor.setDescription(doctor_json.getString("description"));
						boolean bool= service.save(doctor);
						if (bool) {
							map.put("code", 200);
							map.put("msg", "成功");
							renderJson(map);
						} else {
							map.put("code", -1);
							map.put("msg", "失败");
							renderJson(map);
							return;
						}
					}
				}
			}
		}
		renderJson(map);
	}
}
		/*String login_id=getPara("login_id");
		
		if(ToolString.isBlank(login_id)){
			map.put("code", -1);
			map.put("msg", "请传入医生编号");
			renderJson(map);
			return;
		}
		String name=getPara("name");
		if(ToolString.isBlank(name)){
			map.put("code", -1);
			map.put("msg", "请传入医生姓名");
			renderJson(map);
			return;
		}
		String department=getPara("department");
		if(null==department){
			department=name;
		}
		
		String title=getPara("title");
		
		Doctor d=new Doctor();
		Doctor doctor_login_id=service.findByLogin_id(login_id);
		if(null!=doctor_login_id&&doctor_login_id.getDoctor_id()!=null){
			boolean bool= service.updateBy("name='"+name+"',department='"+department+"',title='"+title+"'", " where login_id in('"+login_id+"')", null);
			if(bool){
				map.put("code", 200);
				map.put("msg", "医生信息修改成功");
				renderJson(map);
			}
			return;
		}else{
			d.setLogin_id(login_id);
			d.setName(name);
			d.setDepartment(department);
			d.setTitle(title);
			boolean temp=service.save(d);
			if (temp) {
				map.put("code", 200);
				map.put("msg", "保存成功");
				renderJson(map);
			} else {
				map.put("code", -1);
				map.put("msg", "保存失败");
				renderJson(map);
				return;
			}
		}
		
		renderJson(map);
	}*/
